<template>
  <el-dialog
    :title="serverInfo.name"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div style="width:100%;height:600px;overflow-y: auto;border: 1px solid #cccccc;padding:20px;">
      <div v-for="(msg, index) in messages" :label="index" :key="index">
        <div v-if="msg.type===1" style="font-weight:600;">{{msg.msg}}</div>
        <div v-if="msg.type===0" style="color:red" >{{msg.msg}}</div>
        <div v-if="msg.type===2">{{msg.msg}}</div>
      </div>
      <div><span style="color:#0000ff;font-size: 18px;font-weight: 700;">{{pwd}}:</span><input v-model="cmd" @keyup.enter="submit" style="width: 90%;border:none; outline:medium" /></div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="messages = []">清屏</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import websocket from '../../../utils/websocket.js'
  import Vue from 'vue'
  export default {
    data () {
      return {
        visible: false,
        serverInfo:{},
        cmd:"",
        pwd:"",
        messages:[]
      }
    },
    methods: {
      init (serverInfo) {
        var that=this
        this.serverInfo = serverInfo
        this.visible = true
        var url = "/ssh/"+this.serverInfo.id+"/"+Vue.cookie.get('token')+"?token="+Vue.cookie.get('token');
        websocket.initWebSocket(url,function onmessage(e){
            console.log(e.data)
            var objs = JSON.parse(e.data)
            if(objs.returnCode>0){
              var msgs = objs.stdOutput.split('\n')
              for(var m in msgs){
                that.messages.push({msg:msgs[m],type:0})
              }
            }else{
              var msgs = objs.stdOutput.split('\n')
              for(var m in msgs){
                that.messages.push({msg:msgs[m],type:1})
              }
              that.pwd = objs.pwd
            }

        });
      },
      submit(){
          this.messages.push({msg:this.pwd+":"+this.cmd,type:2})
          websocket.webSocketSend(this.cmd)
          this.cmd=""
      }
    }
  }
</script>
